import { Component } from '@angular/core';
import { NavController, NavParams, LoadingController } from 'ionic-angular';
import { FormGroup, FormBuilder, Validators } from "@angular/forms";
import { UserProvider } from "../../../providers/service/user";

/**
 * Generated class for the ChangePwdPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@Component({
    selector: 'page-change-pwd',
    templateUrl: 'change-pwd.html',
})
export class ChangePwdPage {

    constructor(public navCtrl: NavController,
        public formBuilder: FormBuilder,
        public loadingCtrl: LoadingController,
        public userProvider: UserProvider,
        public navParams: NavParams) {
    }


    msgerr = "";
    new_password: any;

    accountForm: FormGroup;

    ionViewDidLoad() {
        console.log('ionViewDidLoad ChangePwdPage');
        this.accountForm = this.formBuilder.group({
            "old_password": ["", [Validators.required, Validators.minLength(6)]],
            "new_password": ["", [Validators.required, Validators.minLength(6)]],
            "confirm_password": ["", [Validators.required, Validators.minLength(6)]]
        });
        this.new_password = this.accountForm.controls["new_password"];
    }


    change(value) {
        if (value.new_password != value.confirm_password) {
            this.msgerr = "两次密码不相同";
            return;
        }

        this.msgerr = "";

        let loading = this.loadingCtrl.create({
            content: '修改密码操作中，请稍等'
        });
        loading.present();
        var obj = { 
            password: value.old_password,
            newPassword: value.new_password,
            confirmPassword: value.confirm_password
         };
        this.userProvider.changePwd(obj).then(data => {
            loading.setContent("修改成功");
            setTimeout(() => {
                loading.dismiss();
                this.navCtrl.pop();
            }, 500);
        }, err => {
            loading.dismiss();
            this.msgerr = err;
        });


    }
}
